package com.example.kotlindemo.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.kotlindemo.R
import com.example.kotlindemo.bean.StudentBean

/**
 * @功能:
 * @Creat 2020/4/12 20:37
 * @User Lmy
 * @Compony JinAnChang
 */
class RecyclerviewAdapter(val studentList: ArrayList<StudentBean>) :
    RecyclerView.Adapter<RecyclerviewAdapter.ViewHolder>() {
    /**
     * 内部类使用inner关键词修饰
     *
     */
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvname: TextView = itemView.findViewById(R.id.tv_name)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.ecycler_item, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount() = studentList.size
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = studentList[position]
        holder.tvname.text = data.name
        holder.tvname.setOnClickListener {
            onclick?.RvClick(position)
        }
    }

    interface Onclick {
        fun RvClick(pos: Int)
    }

    var onclick: Onclick? = null
    fun setOnclickRv(onclicks: Onclick?) {
        this.onclick = onclicks
    }


}



